﻿(function () {
    var Tree = function (pContainer) {
        var _container = pContainer,
                fold = function (pDom) {
                    if (pDom.innerHTML === "+") {
                        pDom.innerHTML = "-";
                        pDom.parentNode.parentNode.getElementsByTagName("UL")[0].style.display = "";
                    } else {
                        pDom.innerHTML = "+";
                        pDom.parentNode.parentNode.getElementsByTagName("UL")[0].style.display = "none";
                    }
                },
                selectAll = function (pDom) {
                    if (pDom.getAttribute("data-all") != null) {
                        var _root = pDom.parentNode.parentNode.parentNode,
                            _allInput = _root.getElementsByTagName("INPUT"),
                            _check = pDom.checked;
                        for (var i = 0, l = _allInput.length; i < l; i++) {
                            _allInput[i].checked = _check;
                        }
                    }
                },
                init = function () {
                    $(_container).bind("click", function (event) {
                        var me = this,
                            _target = event.target;
                        switch (_target.tagName) {
                            case "A": fold.call(me, _target); break;
                            case "INPUT": selectAll.call(me, _target); break;
                        }
                    });
                };
        init();
    },
        Zip = function (pContainer, checkboxContainer) {
            var _container = pContainer,
                _btnZip = _container.getElementsByTagName("INPUT")[0],
                _selectorZip = _container.getElementsByTagName("SELECT")[0],
                _url = "ZipHandleSingle.ashx",
                _checkboxs = checkboxContainer.getElementsByTagName("INPUT"),
                _loadingPanel = $("#LoadingPanel"),
                _loadingTip = _loadingPanel.find("STRONG"),
                _zipArray = [],
                _resultPanel = $("#result"),
                _resultH3 = _resultPanel.find("H3")[0],
                _resultErrContainer = _resultPanel.find("UL")[0],
                _errList = [],
                _done = 0,
                zip = function () {
                    _loadingPanel.mask();
                    _errList = [];
                    _done = 0;
                    var me = this,
                        _length = _zipArray.length;
                    for (var i = 0; i < _length; i++) {
                        (function (i) {
                            $.ajax({
                                url: _url,
                                scriptCharset: "utf-8",
                                dataType: "json",
                                type: "post",
                                data: { "filePath": _zipArray[i], "mode": _selectorZip.value },
                                success: function (data) {
                                    !data.type && _errList.push({ file: _zipArray[i], err: data.err });
                                    _done++;
                                    _loadingTip.innerHTML = ["压缩中，请稍后...(", _done, "/", _length, ")"].join("");
                                    if (_done === _length) {
                                        _loadingPanel.unmask();
                                        showResult.call(me);
                                    }
                                }
                            });
                        })(i);
                    }
                },
                showResult = function () {
                    _resultH3.innerHTML = ["本次共尝试压缩", _zipArray.length, "个文件，成功", _done, "个文件"].join("");
                    _resultErrContainer.innerHTML = "";
                    var _sb = [];
                    for (var i = 0, l = _errList.length; i < l; i++) {
                        _sb.push("<li><h4>");
                        _sb.push(_errList[i].file);
                        _sb.push("</h4><div><p>");
                        _sb.push(_errList[i].err);
                        _sb.push("</p></div></li>");
                    }
                    _resultErrContainer.innerHTML = _sb.join("");
                },
                getArray = function () {
                    _zipArray = [];
                    for (var i = 0, l = _checkboxs.length; i < l; i++) {
                        if (_checkboxs[i].getAttribute("data-all") == null && _checkboxs[i].value != null && _checkboxs[i].checked) {
                            _zipArray.push(_checkboxs[i].value);
                        }
                    }
                },
                init = function () {
                    var me = this;
                    $(_btnZip).bind("click", function () {
                        getArray.call(me);
                        if (_zipArray.length === 0) {
                            alert("同学，你还没有选择要压缩的文件！");
                        } else {
                            zip.call(me);
                        }
                    });
                };
            init();
        };

    $(document).ready(function () {
        var _treeContainer = document.getElementById("rootContainer"),
            _submitContainer = document.getElementById("submitContainer");
        new Tree(_treeContainer);
        new Zip(_submitContainer, _treeContainer);
    });
})();